// import * as types from '../mutation-types'

// export default {
//   state,
//   getters,
//   actions,
//   mutations
// }
import { invoke } from '@/api/base.js'
const entityList = {
  // initial state
  state: {
    clientData: {},
    schemaId: null,
    schemaItems: [],
    showSidebar: false
  },

  // getters
  getters: {
    getListClientData: state => state.clientData,
    getListSchemaId: state => state.schemaId,
    getListSchemaItems: state => state.schemaItems,
    getListSidebarVisible: state => state.showSidebar
  },

  // mutations
  mutations: {
    SET_LIST_CLIENT_DATA: (state, clientData) => {
      state.clientData = clientData
    },
    SET_LIST_SCHEMA_ID: (state, schemaId) => {
      state.schemaId = schemaId
    },
    SET_LIST_SCHEMA_ITEMS: (state, schemaItems) => {
      state.schemaItems = schemaItems
    },
    SET_LIST_SIDEBAR_VISIBLE: (state, showSidebar) => {
      state.showSidebar = showSidebar
    }
  },

  // actions
  actions: {
    setListClientData ({commit}, clientData) {
      commit('SET_LIST_CLIENT_DATA', clientData)
    },
    setListSchemaId ({commit}, schemaId) {
      commit('SET_LIST_SCHEMA_ID', schemaId)
    },
    setListSchemaItems ({commit}, schemaItems) {
      commit('SET_LIST_SCHEMA_ITEMS', schemaItems)
    },
    setListSidebarVisible ({commit}, showSidebar) {
      commit('SET_LIST_SIDEBAR_VISIBLE', showSidebar)
    },
    showSidebar ({commit}, visible) {
      commit('SET_LIST_SIDEBAR_VISIBLE', visible)
    },
    // load items
    loadSchemaItems ({commit, state}) {
      return new Promise((resolve, reject) => {
        invoke('Ecp.QuerySchema.getItemsData', {unitId: state.clientData.unitId})
          .then((res) => {
            const items = res.data || []
            commit('SET_LIST_SCHEMA_ITEMS', items)
            resolve()
          }).catch(error => {
            reject(error)
          })
      })
    }
  }
}

export default entityList
